% This code computes for each individual, 
% 1) the value from not purchasing any insurance,
% 2) for each 2nd period state, the 2nd period flow utility from lapsing
% the contract. 

% size(v0) = N_y x 1
% size(U0) = N_y x K
function [v0, U0] = consumer_v0_ftn(N_y, rho, crra, beta_c, K,  ...
    pr_s, x_fc,  pr_fc, y_medi, B1_c, B2_c, T1, alpha,...
    lapse_u, resource_y1, resource_y2)

% stack into N_y*K rows
RESOURCE_y2 = kron(resource_y2,ones(K,1));
X_fc  = repmat(repmat(x_fc, K,1),N_y,1); 
Pr_fc = kron([pr_fc 1-pr_fc], ones(K,1));

% income after paying for formal care using OOP money
Y_fc = y_fc_ftn(RESOURCE_y2, X_fc, y_medi); 

% uninsured utility conditional on the realization of formal care risk in t=2
U = [u_c(Y_fc,rho,crra) u_c(RESOURCE_y2,rho,crra)]; 

% uninsured utility conditional on K states in t=2
U0 = (U.*Pr_fc)*ones(2,1);
U0 = (reshape(U0,K,N_y))'; 
U0 = alpha*B2_c*U0;       

% integrate over K
EU0 = U0*pr_s; 

% value from being uninsured 
v0 = B1_c*alpha*u_c(resource_y1,rho,crra) + (beta_c^T1)*EU0;

% second-period utility conditional on lapsing
U0 = U0 + lapse_u;